#ARGV 0:	directory of log files
#ARGV 1:	number of considered records

#need to set a parameter that minimum time difference between two sequential rounds, indicating there is missing round. 
$baseRound = 1;

my $startTime = time;
opendir $dir, $ARGV[0] or die "can not open the directory $ARGV[0]!\n";


$roundNum = 1;
if($ARGV[1]!=0){
$roundNum = $ARGV[1];
}

@validRound;
@indexRound;
@timeRound;

for($i=0;$i<16;$i++){
	for($j=$baseRound;$j<=$baseRound+$roundNum;$j++){
		$validRound[$i][$j]=1;
		$timeRound[$i][$j] = 0;
	}
	$indexRound[$i] = 1;
}


print "Step1: reading in the round information\n";
while ($file = readdir $dir) {
	$file_path = "$ARGV[0]\\$file";
	#print ("$file_path  \n $file\n");
	
	$logfile = "\.log";
	#get input samples
	$readinRound = 0;
	if($file =~ /$logfile/) {
		print ("$file_path  \n $file\n");
		open fin, "<$file_path" or die "can not open file $file_path!\n";
		$channel = -1;
		$fir = 1;
		while (<fin>) {
			chomp;
			@info = split/,/;
			
			if($readinRound==$roundNum){
				last;
			}
			
			if($info[0] eq "infoc"){
				$channel = $info[1]-11;
			}
			if($info[0] eq "infos"){
				$timeRound[$channel][$info[1]] = $info[2];
				if($fir==1){
					$indexRound[$channel]=$info[1];
					$fir=0;
				}
				$readinRound++;
				#print "$channel $info[1] $timeRound[$channel][$info[1]]\n";
			}
		}
	}
	else {
		#print("No log files found!\n");
		next;
	}
}

print "Step2: mark the invalid rounds\n";
for($j=$baseRound;$j<=$baseRound+$roundNum;$j++){
	#print "forloop $j $timeRound[0][$j] ";
	#if($timeRound[0][$j]!=0){
		#use channel 11 as the reference starting time of each round.
		#first make sure channel 11 does not lose info this round.
		
		$existValid = 0;
		#if starting time is larger than previous one 10000, then this round lost info.
		#$temp = ($timeRound[$m][$indexRound[$m]] - $timeRound[$m][$indexRound[$m]-1]);
		#print "temp: $timeRound[$m][$indexRound[$m]] $timeRound[$m][$indexRound[$m]-1] $temp\n";
		
		# while(($timeRound[$m][$indexRound[$m]] - $timeRound[$m][$indexRound[$m]-1]) > $diffTime 
		  # && ($timeRound[$m][$indexRound[$m]] - $timeRound[$m+1][$indexRound[$m+1]]) > 100 ){
			# $validRound[$m][$indexRound[$m]] = 0;
			# $existValid = 1;
			# $m++;
		# }
		$min = 1000000000;
		for($i=0;$i<16;$i++){
			if($timeRound[$i][$indexRound[$i]]<$min){
				$min = $timeRound[$i][$indexRound[$i]];
			}
		}
		print "forloop $j $min ";
		for($i=0;$i<16;$i++){
			print "$timeRound[$i][$indexRound[$i]] ";
			if(($timeRound[$i][$indexRound[$i]]-$min)>150){
				$validRound[$i][$indexRound[$i]] = 0;
				$existValid = 1;
			}
		}
		print "$existValid \n";
		for($i=0;$i<16;$i++){
			if($existValid==1){
				if($validRound[$i][$indexRound[$i]] == 0){
					$validRound[$i][$indexRound[$i]] = 1;
				}
				else {
					$validRound[$i][$indexRound[$i]] = 0;
					$indexRound[$i]++;
				}
			}
			else{
				$indexRound[$i]++;
			}
		}
		
	#}
}


for($j=$baseRound;$j<=$baseRound+$roundNum;$j++){
	print "$j ";
	for($i=0;$i<16;$i++){
		if($validRound[$i][$j]==0){
			print "$i ";
		}
	}
	print "\n";
}



print "Step3: write the logs into filtering files\n";
opendir $dir, $ARGV[0] or die "can not open the directory $ARGV[0]!\n";


open debug, ">$ARGV[0]\\filter\\debug.info" or die "can not open file .\\$file\n";

while ($file = readdir $dir) {
	$file_path = "$ARGV[0]\\$file";
	#print ("$file_path  \n $file\n");
	
	$logfile = "\.log";
	$lastinfoc = "";
	#get input samples
	if($file =~ /$logfile/) {
		open fin, "<$file_path" or die "can not open file $file_path!\n";
		open fout, ">$ARGV[0]\\filter\\$file" or die "can not open file .\\$file\n";
		print ("$file\n");
		$channel = -1;
		$round = -1;
		$readinRound = 0;
		while (<fin>) {
			chomp;
			@info = split/,/;
			
			if($readinRound==$roundNum){
				last;
			}
			
			if($info[0] =~ "Connecting"){
				next;
			}
			
			if($info[0] eq "infoc"){
				$lastinfoc = $_;
				$channel = $info[1]-11;
				#print "$lastinfoc\n";
			}
			if($info[0] eq "infos"){
				$round = $info[1];
				if($validRound[$channel][$round]!=0){
					print fout "$lastinfoc\n";
					print fout $_;
					print fout "\n";
					print debug "$timeRound[$channel][$round] ";
				}
				#print "$channel $info[1] $info[2]\n";
			}
			
			if($info[0] ne "infoc" && $info[0] ne "infos" && $validRound[$channel][$round]!=0){
				print fout $_;
				print fout "\n";
			}
			
			if($validRound[$channel][$round]!=0 && $info[0] eq "infoe"){
					$readinRound++;
			}
		}
		print debug "\n";
		close fin;
		close fout;
	}
	else {
		#print("No log files found!\n");
		next;
	}
}

close debug;

my $duration = time - $startTime;
print "RUN $duration\n";	